﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageSalesApprovedFee : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private SalesFeeEntity mSelectedSalesFee;
        #endregion

        #region Constructor
        public frmManageSalesApprovedFee()
        {
            InitializeComponent();
            if (CommonParameter.CurrentUser.PersonEntity != null)
            {
                txtPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                txtPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn;
            }
        }
        public frmManageSalesApprovedFee(MstCustomerEntity customer)
        {
            InitializeComponent();
            txtCustomer.Text = customer.CustomerName;
            txtCustomer.Tag = customer;
        }
        public frmManageSalesApprovedFee(MstPersonEntity person)
        {
            InitializeComponent();
            txtPerson.Text = person.NameCn;
            txtPerson.Tag = person;
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND F.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (dtpFeeDate.EditValue!=null)
            {
                condition += " AND F.END_DATE >= CONVERT(DATETIME,'" + dtpFeeDate.EditValue.ToString() + " 00:00:00')";
            }
            if (dtpFeeDateTo.EditValue != null)
            {
                condition += " AND F.START_DATE <= CONVERT(DATETIME,'" + dtpFeeDateTo.EditValue.ToString() + " 23:59:59')";
            }
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtPerson.Tag;
                condition += " AND F.PERSON_ID = '" + entity.PersonId + "' ";
            }
            else
            {
                if (!mCanQueryOtherPerson)
                {
                    condition += " AND F.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + "' ";
                }
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "P.NAME_CN, C.CUSTOMER_NAME";
            DataTable dt = SalesApprovedFeeBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.销售);
            BindDataGrid();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Text = frm.SelectedPerson.NameCn;
                txtPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPerson.Text = "";
                txtPerson.Tag = null;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        #endregion
    }
}
